/*
 * @Author: 许旺玉
 * @Date: 2020-07-07 11:24:48
 * @LastEditTime: 2020-07-10 10:19:27
 * @LastEditors: 範遠茂
 * @Description: 路由搭建
 * @FilePath: \靖凯开源系统\jingkaisystem\src\router\index.js
 */

import Vue from "vue"
import VueRouter from "vue-router" //引入路由
import routes from "./routes.js" //引入路由列表
import store from "../store/index.js"
import Element from 'element-ui';

Vue.use(VueRouter)

// 解决路由重复点击问题
const originalPush = VueRouter.prototype.push
   VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
	routes
})


//路由前置守卫
router.beforeEach(function (to, from, next) {
	// console.log(examToken)
	// console.log(to.matched[0].meta.requiresAuth)
	if (!to.matched[0].meta.requiresAuth) { //判断路由元信息
		next()
	} else {
		let examToken = store.state.loginModule.examToken //获取vuex的token
		if (!examToken) { //判断是否有token
			next({ path: "/login" }) 
			//提示信息
			Element.Message.error('请登录');
		} else {
			next()
		}
	}
})

export default router